var regcard = /^(\d{16}|\d{19})$/;
var regphone = /^1[3|4|5|7|8]\d{9}$/;
var savebtn = false;
var flag = false;
function save_banding() {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = '/except/login.jsp';
        return;
    }
    var jsonstr = "{token:" + token + "}";
    if (savebtn)
        return;
    else {
        savebtn = true;
        var name = $("#name").val();
        var bankname = $("#bank_name").val();
        var bankaddress = $("#bank_address").val();
        var card = $("#card").val();
        var phone = $("#phone").val();
        if (isempty(name)) {
            $("#hintbg").show();
            $("#hint_message").html("请输入名字");
            savebtn = false;
            return;
        }
        if (isempty(bankname)) {
            $("#hintbg").show();
            $("#hint_message").html("请输入所属银行名称");
            savebtn = false;
            return;
        }
        if (isempty(bankaddress)) {
            $("#hintbg").show();
            $("#hint_message").html("请输入开户行地址");
            savebtn = false;
            return;
        }
        if (isempty(card)) {
            $("#hintbg").show();
            $("#hint_message").html("请输入银行卡号");
            savebtn = false;
            return;
        } else if (!regcard.test(card)) {
            $("#hintbg").show();
            $("#hint_message").html("请输入正确格式的银行卡号");
            savebtn = false;
            return;
        }
        if (isempty(phone)) {
            $("#hintbg").show();
            $("#hint_message").html("请输入手机");
            savebtn = false;
            return;
        } else if (!regphone.test(phone)) {
            $("#hintbg").show();
            $("#hint_message").html("请输入正确格式的手机号");
            savebtn = false;
            return;
        }
        var url = "/except/bankcontroller/bindingbank.action";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                json: jsonstr,
                name : name,
                bankname:bankname,
                bankaddress:bankaddress,
                card:card,
                phone:phone
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message
                if (code == 20000) {
                    $("#hintbg").show();
                    $("#hint_message").html("绑定成功");
                    flag = true;
                } else if(code == 10001 || code == 10002)
                    location.href = '/except/login.jsp';
                else {
                    $("#hintbg").show();
                    $("#hint_message").html(message);
                    savebtn = false;
                }
                remove_advertising();
            }
        });
    }
}

$(function () {
    $("#confirm_btn").click(function () {
        if(flag) {
            var forword_type = $("#forword_type").val();
            location.href = "/except/files/user/binding_bank.jsp?forword_type=" + forword_type;
        }
    });
});
